/*
 * Copyright (C) 2011 The Code Bakers
 * Authors: Cleuton Sampaio e Francisco Rodrigues
 * e-mail: thecodebakers@gmail.com
 * Project: http://code.google.com/p/biomago/
 * Site: http://www.thecodebakers.org
 *
 * Licensed under the GNU GPL, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://gplv3.fsf.org/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Cleuton Sampaio e Francisco Rogrigues - thecodebakers@gmail.com
 */

package br.com.thecodebakers.biomago.activities;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import br.com.thecodebakers.biomago.R;
import br.com.thecodebakers.biomago.business.BioBO;
import br.com.thecodebakers.biomago.business.BioCalculator;
import br.com.thecodebakers.biomago.graphics.BioGraphicView;
import br.com.thecodebakers.biomago.model.BioPlot;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class GrafBio extends Activity {
    private static final String TAG = "GrafBio";
    private BioGraphicView imagem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "Entrou no create da GrafBio");
		Resources res = getResources();
		imagem = new BioGraphicView(this.getApplicationContext());
		Bundle extras = getIntent().getExtras();
		LinearLayout layout = new LinearLayout(this.getApplicationContext());
				
		layout.setOrientation(layout.VERTICAL);
		if (extras != null) {
			imagem.setNome(extras.getString("nome"));
			imagem.setData(extras.getString("data"));
			imagem.setTitBio(res.getString(R.string.titBio));
			imagem.setTitNascimento(res.getString(R.string.titNascimento));
			imagem.setTitDataInicial(res.getString(R.string.titDataInicial));
			imagem.setTitEmocional(res.getString(R.string.titEmocional));
			imagem.setTitFisico(res.getString(R.string.titFisico));
			imagem.setTitIntelectual(res.getString(R.string.titIntelectual));
			imagem.setMensagem(res.getString(R.string.mensagemgrafico));
			SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
			sdf.applyPattern("dd/MM/yyyy");
			try {
				Date data = sdf.parse(imagem.getData());
				List<BioPlot> pontos = BioCalculator.getDateList(data, 30);
				imagem.setLista(pontos);
				
				imagem.setBackgroundColor(Color.WHITE);

			} catch (ParseException e) {
				Log.e(TAG, "Erro ao formatar a data. Exception.");
			}

		    AdView adView = new AdView(this, AdSize.BANNER, res.getString(R.string.MY_ID));
		    layout.addView(adView);
		    adView.loadAd(new AdRequest());

		    layout.addView(imagem);

			this.setContentView(layout);
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu_grafico, menu);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnuGrafEmail:
			this.enviarBitmap();
			return true;
		case R.id.mnuAjuda:
			Intent i = new Intent (this.getApplicationContext(), Ajuda.class);
			this.startActivity(i);
			return true;
		default: return super.onOptionsItemSelected(item); 
		}
	}

	private void enviarBitmap() {
		/*
		 * Salva o bitmap e invoca outra activity
		 */
		Resources res = getResources();
		FileOutputStream fos = null;
        try {
        	fos = new FileOutputStream("/sdcard/BIOMAGO_TEMPIMG.png");
        } 
        catch (FileNotFoundException e) {
        	e.printStackTrace();
        }
        imagem.getFundo().compress(Bitmap.CompressFormat.PNG, 50, fos);

       try {
    	   fos.close();
    	   final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    	   emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+"/sdcard/BIOMAGO_TEMPIMG.png"));
    	   emailIntent.setType("image/png");
           this.startActivity(Intent.createChooser(emailIntent, res.getString(R.string.textochooser)));
       } 
       catch (IOException e) {
    	   e.printStackTrace();
       }
	}
	
}
